package com.tgpms.interfaces.Request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * Created By Riven on 2020-9-14
 */
public class HbHttpClient {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final RestTemplate client = new RestTemplate(request());

    // 默认一个随机码，用于判断时效
    private static String accessToken = "123";

    public static SimpleClientHttpRequestFactory request() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(60 * 1000);
        requestFactory.setReadTimeout(60 * 1000);
        return requestFactory;
    }

    public String login(String url, Map<String, Object> param, HttpMethod method) {
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        for (Map.Entry<String, Object> keys : param.entrySet()) {
            map.add(keys.getKey(), keys.getValue());
        }
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = null;
        try {
            response = client.exchange(url, method, requestEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        JSONObject parseObject = null;
        parseObject = JSON.parseObject(response.getBody());
        if (parseObject.get("success").equals(true)) {
            accessToken = parseObject.get("access_token").toString();
        }
        return response != null ? response.getBody() : null;
    }

    public String post(String url, Map<String, Object> param, HttpMethod method) {
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        for (Map.Entry<String, Object> keys : param.entrySet()) {
            map.add(keys.getKey(), keys.getValue());
        }
        // 设置头部请求
        headers.set("access_token", accessToken);
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        //将请求头部和参数合成一个请求
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = null;
        try {
            response = client.exchange(url, method, requestEntity, String.class);
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return response != null ? response.getBody() : null;
    }
}
